<?php

/**
 * This is the model class for table "bookmark".
 *
 * The followings are the available columns in table 'bookmark':
 * @property integer $id
 * @property string $url
 * @property string $title
 * @property integer $create_user_id
 * @property integer $bookmark_folder_id
 *
 * The followings are the available model relations:
 * @property BookmarkFolder $folder
 * @property User $owner
 */
class Bookmark extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Bookmark the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'bookmark';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
            array('url, title', 'required'),
            array('bookmark_folder_id', 'numerical', 'integerOnly' => true),
            array('url, title', 'length', 'max' => 255),
            // url must be a valid url
            array('url', 'validUrl'),
            // The following rule is used by search().
            array('url, title, bookmark_folder_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * check whether the url is a valid url
     */
    public function validUrl($attribute, $params) {
        $regex = "((https?|ftp)\:\/\/)?"; // SCHEME
        $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
        $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
        $regex .= "(\:[0-9]{2,5})?"; // Port
        $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
        $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
        $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor

        if (!preg_match("/^$regex$/", $this->url)) {
            $this->addError('url', 'Please enter a valid url');
        }
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        return array(
            'folder' => array(self::BELONGS_TO, 'BookmarkFolder', 'bookmark_folder_id'),
            'owner' => array(self::BELONGS_TO, 'User', 'create_user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'url' => 'Url',
            'title' => 'Title',
            'create_user_id' => 'Owner',
            'bookmark_folder_id' => 'Bookmark Folder',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        $criteria = new CDbCriteria;

        $criteria->compare('url', $this->url, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('bookmark_folder_id', $this->bookmark_folder_id);
        $criteria->compare('create_user_id', Yii::app()->user->id);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Set the value of attributes 'create_user_id' before the form is validated.
     */
    protected function beforeValidate() {
        if ($this->isNewRecord) {
            $this->create_user_id = Yii::app()->user->id;
        }

        return parent::beforeValidate();
    }

}